#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
 @Author: Monox
 @Email: 627201584@qq.com
 @FileName: InternalExecution.py
 @DateTime: 2023/2/23 15:21
 @SoftWare: PyCharm
"""
import BackEnd
import PySimpleGUI as sg
import pandas as pd
from datetime import datetime

def readExcel_pandas(path):
    """
    003
    读取EXCEL物流单号文件
    """
    excel_file = path  # 文件变量
    data = pd.read_excel(excel_file)  # 读取EXCEL文件
    express_noList = data['物流单号'].values  # EXCEL文件中指定列转为列表
    # print(datalist)
    print(f'函数readExcel_pandas：一共有{len(express_noList)}张物流单号')
    return express_noList

def log(logdata):
    """
    003
    日志函数
    """
    with open("log.txt", "a") as log:
        log.write(f'时间:{datetime.now()},日志:{logdata}\n\n')

def paichong_main(path, progress_bar, remarks, warehousein_code, Submit, Import, GYurl, GYappkey, GYsecret, GYsessionkey):
    """
    排重物流单号
    main函数
    """
    print(f'函数paichong_main：排重物流单号的进程')
    express_no_list = readExcel_pandas(path)  # 读取物流单号表格
    progress_bar.UpdateBar(0, len(express_no_list) + 1)  # 更新进度条长度， progress_bar为前端变量
    i = 1  # 进度
    for express_no in express_no_list:  # 遍历物流单号列表
        express_no = str(express_no)
        # 判断排重
        backend = BackEnd.GY_API(GYurl, GYappkey, GYsecret, GYsessionkey)
        returnget_response = backend.returnget(express_no)  # 调用退货单查询，入参为物流单号
        returnget_total = returnget_response.get('total')  # 得到returnget_response的总条数
        print(f'函数paichong_main：EXCEL的第{i + 1}行,物流单号{express_no}')  # 第一个物流单号 = EXCEL的第二行
        if returnget_total > 0:
            logodata = (f"失败：物流单号\'{express_no}\'系统退货单内已重复存在")
            log(logodata)
            print(logodata)
            sg.cprint(logodata)  # GUI打印更改到这里使用
            i = i + 1
            progress_bar.UpdateBar(i)
        else:
            BackEnd.GYAPI_main(express_no, remarks, warehousein_code, GYurl, GYappkey, GYsecret, GYsessionkey)
            i = i + 1
            progress_bar.UpdateBar(i)

        if i == len(express_no_list) + 1:
            Submit.update(disabled=False)
            Import.update(disabled=False)
            # window2.write_event_value("任务已完成", "OK")  # 多线程的话，我们需要让函数通知主窗体一个事件，当完成运算时发送一个事件来通知结果：


def NO_paichong_main(path, progress_bar, remarks, warehousein_code, Submit, Import, GYurl, GYappkey, GYsecret,
                  GYsessionkey):
    """
    不排重物流单号
    main函数
    """
    print(f'函数paichong_main：不排重物流单号的进程')
    express_no_list = readExcel_pandas(path)  # 读取物流单号表格
    progress_bar.UpdateBar(0, len(express_no_list) + 1)  # 更新进度条长度， progress_bar为前端变量
    i = 1  # 进度
    for express_no in express_no_list:  # 遍历物流单号列表
        express_no = str(express_no)
        print(f'函数NO_paichong_main：EXCEL的第{i + 1}行,物流单号{express_no}')  # 第一个物流单号 = EXCEL的第二行
        BackEnd.GYAPI_main(express_no, remarks, warehousein_code, GYurl, GYappkey, GYsecret, GYsessionkey)
        i = i + 1
        progress_bar.UpdateBar(i)

        if i == len(express_no_list) + 1:
            print(f'函数NO_paichong_main：i == len(express_no_list) + 1 ;i={i}')
            Submit.update(disabled=False)
            Import.update(disabled=False)
            # window2.write_event_value("任务已完成", "OK")  # 多线程的话，我们需要让函数通知主窗体一个事件，当完成运算时发送一个事件来通知结果：



if __name__ == '__main__':
    path = 'C:/Users/陈志豪/Desktop/湾豚自动新增退货单管易专版9.1.1 (示例)/妮趣.xls'
    remarks = '2023-02-23工具导入'
    warehousein_code = ''
    GYurl = 'http://v2.api.guanyierp.com/rest/erp_open'
    GYappkey = 'XXX'
    GYsecret = 'XXX'
    GYsessionkey = 'XXX'
    NO_paichong_main(path=path, remarks=remarks, warehousein_code=warehousein_code, GYurl=GYurl, GYappkey=GYappkey, GYsecret=GYsecret, GYsessionkey=GYsessionkey)


